package com.cloud.job.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.api.system.SystemTestFeign;
import com.cloud.api.system.UserFeign;
import com.cloud.api.system.model.dto.UserDTO;
import com.cloud.api.system.model.parm.UserPARM;
import com.cloud.common.core.asserts.ApiAssert;
import com.cloud.common.core.enums.AuthEnums;
import com.cloud.common.core.enums.ErrorCodeEnum;
import com.cloud.common.core.model.response.ApiResponses;
import com.cloud.common.core.model.supers.SuperController;
import com.cloud.common.log.annotation.Log;
import com.cloud.common.log.enums.BusinessType;
import com.cloud.common.security.annotation.PreAuth;
import com.cloud.job.project.service.ITestService;
import com.cloud.tools.log.project.model.entity.OperateLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * feignTest 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2021-02-08
 */
@Slf4j
@Api(tags = {"feignTest"})
@RestController
@RequestMapping("/open/test")
public class TestRestController extends SuperController {

    @Autowired
    private ITestService testService;
    @Resource
    private UserFeign userFeign;
    @Resource
    private SystemTestFeign testFeign;

    @PreAuth(AuthEnums.OPEN)
    @Log(title = "log测试Get", businessType = BusinessType.SELECT)
    @ApiOperation("log测试Get")
    @GetMapping("/logGetTest")
    public ApiResponses<String> logGetTest(@RequestParam("str1") String str1, @RequestParam("str2") String str2) {
        return success(str1 + str2);
    }

    @PreAuth(AuthEnums.OPEN)
    @Log(title = "log测试PostBody", businessType = BusinessType.INSERT)
    @ApiOperation("log测试PostBody")
    @PostMapping("/logPostBodyTest")
    public ApiResponses<OperateLog> logPostBodyTest(@RequestBody OperateLog operateLog) {
        return success(operateLog);
    }

    @PreAuth(AuthEnums.OPEN)
    @Log(title = "log测试PostParam", businessType = BusinessType.INSERT)
    @ApiOperation("log测试PostParam")
    @PostMapping("/logPostParamTest")
    public ApiResponses<String> logPostParamTest(@RequestParam("str1") String str1, @RequestParam("str2") String str2) {
        return success(str1 + str2);
    }

    @PreAuth(AuthEnums.OPEN)
    @Log(title = "log测试Error", businessType = BusinessType.OTHER)
    @ApiOperation("log测试Error")
    @PostMapping("/logErrorTest")
    public ApiResponses<OperateLog> logErrorTest(@RequestBody OperateLog operateLog) {
        ApiAssert.failure(ErrorCodeEnum.INTERNAL_SERVER_ERROR.convert("测试异常"));
        return success(operateLog);
    }

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("job模块调用system模块User分页")
    @GetMapping("/remoteUserFeign")
    public ApiResponses<Page<UserDTO>> test() {
        return userFeign.page(new UserPARM());
    }

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("job模块调用system模块test")
    @GetMapping("/remoteTestFeign")
    public ApiResponses<String> testFeign() {
        return testFeign.sysLoad();
    }

    @PreAuth(AuthEnums.OPEN)
    @ApiOperation("分布式seata测试 扣减库存")
    @PostMapping("/seataReduceStore")
    public ApiResponses<Void> seataReduceStore() {
        testService.seataReduceStore();
        return success();
    }
}

